﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using Newtonsoft.Json;
using MessageMedia;
using MessageMedia.Common;
using MessageMedia.Diagnostics;
using MessageMedia.Events;
using MessageMedia.Exceptions;
using MessageMedia.Gateway.ActionResult;
using MessageMedia.SMS;
using System.Configuration;
using CRMBiz;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for SMSHandler
    /// </summary>
    public class SMSHandler : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function


        public bool AddHistory(int app_id, int ContactID, string HistoryDate, string HistoryTime, string Action, string Operator, string Description)
        {
            bool flag = false;
            try
            {
                flag = SMSTemplateHandler.AddHistory(app_id, ContactID, HistoryDate, HistoryTime, Action, Operator, Description);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        public void getAllMobileNo(int ID, int isAppID)
        {
            try
            {
                DataSet ds = SMSTemplateHandler.getAllMobileNo(ID, isAppID);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void CheckMobileNos(string MobileNo)
        {
            try
            {
                int flag = 0;
                string[] mobileno = MobileNo.Split(',');
                for (int i = 0; i < mobileno.Length; i++)
                {
                    string cleanedPhoneNumber;
                    PhoneNumberValidity validity = Messaging.MessageController.PhoneNumberIsValid(mobileno[i], out cleanedPhoneNumber);
                    // Check the validity of the mobile phone number.
                    if (validity != PhoneNumberValidity.Mobile && validity != PhoneNumberValidity.Either)
                        flag = 0; // wrong number
                    else
                        flag = 1; // valid number
                }
                context.Response.Write(flag);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
        public void SendSMS(string MobileNo, string Message, string compnayAccount, string smsPassword)
        {
            try
            {
                context.Response.Write(SmsHelper.SendSMS(MobileNo, Message, compnayAccount, smsPassword));
            }
            catch (Exception ex)
            {
                context.Response.Write("Error");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void getEmailwithFullName(int app_id, int ContactID)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ApplicationContactHandler.BindContactForEmailSMS(app_id, ContactID));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void WriteLogForSMSEmail(string logText)
        {
            try
            {
                context.Response.Write(ErrorHandler.saveSMSEmailLog(logText));
            }
            catch (Exception ex)
            {
                context.Response.Write("0");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
    }
}